/**
 * 
 */
package com.lanxigua.utils;

import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * @author Administrator
 * 
 */
public class Common {

	/**
	 * 修改API动作名称
	 * 
	 * @param Action
	 * @return
	 */
	public static String getActionName(Action Action) {
		String action = Action.toString();
		StringBuilder builder = new StringBuilder();

		for (int i = 0; i < action.length(); i++) {
			char letter = action.charAt(i);

			if (letter >= 'A' && letter <= 'Z' && i != 0) {
				builder.append(".");
			}
			builder.append(String.valueOf(letter));
		}

		return builder.toString().toLowerCase();
	}

	/**
	 * 网络读取图片成Bitmap
	 * 
	 */
	public static Bitmap getImageBitmap(String path) throws Exception {
		Bitmap bm = null;

		String url = "http://www.lanxigua.com";
		URL requestUrl = new URL(url + path);
		bm = BitmapFactory.decodeStream(requestUrl.openStream());

		return bm;
	}
	
	/**
	 * 生成圆角Bitmap
	 * @param bitmap
	 * @return
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
	    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
	        bitmap.getHeight(), Config.ARGB_8888);
	    Canvas canvas = new Canvas(output);
	 
	    final int color = 0xff424242;
	    final Paint paint = new Paint();
	    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
	    final RectF rectF = new RectF(rect);
	    final float roundPx = 12;
	 
	    paint.setAntiAlias(true);
	    canvas.drawARGB(0, 0, 0, 0);
	    paint.setColor(color);
	    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
	 
	    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	    canvas.drawBitmap(bitmap, rect, rect, paint);
	 
	    return output;
	  }
}
